﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Catalog
{
    public partial class FrmCuonSach : PRE.Catalog.FrmBase
    {
        #region Variable
        DAL.EntitiesDAL.CuonSachDAL _dal = new DAL.EntitiesDAL.CuonSachDAL();
        DAL.EntitiesDAL.DauSachDAL _dal2 = new DAL.EntitiesDAL.DauSachDAL();
        DAL.EntitiesDAL.KeSachDAL _dal3 = new DAL.EntitiesDAL.KeSachDAL();
        BLL.EntitiesBLL.CuonSachBLL _bll = new BLL.EntitiesBLL.CuonSachBLL();

        DataTable _dt = new DataTable();
        DataTable _dt2 = new DataTable();
        DataTable _dt3 = new DataTable();


        #endregion

        #region Inits
        public FrmCuonSach()
        {
            InitializeComponent();
        }

        #region Form Load
        private void FrmCuonSach_Load(object sender, EventArgs e)
        {
            PerformRefresh();
        }
        #endregion
        #endregion

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {
            DataTable _ma = _dal.Select_Max();
            if (_ma != null)
                if (_ma.Rows.Count > 0)
                {
                    string ma = _bll.AddID(_ma.Rows[0]["Ma"] + "");
                    txt_MaCuonSach.Text = ma;

                    ReadOnlyControl(false);
                    ClearDataBindings();
                    ChangeStatus(false);


                    cbo_MaDauSach.ResetText();
                    cbo_MaKeSach.ResetText();

                    txt_MaCuonSach.Properties.ReadOnly = true;

                    
                }
            
            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            var id1 = gridView_CuonSach.GetFocusedRowCellValue("MACUONSACH") + "";

            DialogResult result;
            var ok = _dal.Delete(id1);
            result = XtraMessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    XtraMessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);

            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();
            ChangeStatus();
            ResetText();

            btnGoSearch.Visible = false;

            if (_dt != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dt.Rows.Count > 0) DataBindingControl();
            }



            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
            if (IsAdd)
            {
                var o = new DAL.Entities.CuonSach()
                {
                    MACUONSACH = Convert.ToString(txt_MaCuonSach.Text),
                    MADAUSACH = Convert.ToString(cbo_MaDauSach.EditValue),
                    MAKESACH = Convert.ToString(cbo_MaKeSach.EditValue)
                   
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                  //  PerformAdd();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }

            if (IsEdit)
            {
                var id1 = gridView_CuonSach.GetFocusedRowCellValue("MACUONSACH") + "";
                //var id2 = gridView_TacGia.GetFocusedRowCellValue("MalopID") + "";

                var o = new DAL.Entities.CuonSach()
                {
                    MACUONSACH = Convert.ToString(txt_MaCuonSach.Text),
                    MADAUSACH = Convert.ToString(cbo_MaDauSach.EditValue),
                    MAKESACH = Convert.ToString(cbo_MaKeSach.EditValue)
                };

                var oki = _dal.Update(o);

                if (oki)
                {
                    // XtraMessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    XtraMessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {
            txt_MaCuonSach.ResetText();
            cbo_MaDauSach.ResetText();
            cbo_MaKeSach.ResetText();
            txt_TenKeSach.ResetText();
            txt_TenDauSach.ResetText();
            txt_SoTrang.ResetText();
            txt_NoiDungTomTat.ResetText();
            txt_NgonNgu.ResetText();
            txt_NamXuatBan.ResetText();
            txt_LanXuatBan.ResetText();
            txt_KichThuoc.ResetText();

           
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txt_MaCuonSach.DataBindings.Clear();
            cbo_MaDauSach.DataBindings.Clear();
            cbo_MaKeSach.DataBindings.Clear();
            txt_KichThuoc.DataBindings.Clear();
            txt_LanXuatBan.DataBindings.Clear();
            txt_NamXuatBan.DataBindings.Clear();
            txt_NgonNgu.DataBindings.Clear();
            txt_NoiDungTomTat.DataBindings.Clear();
            txt_SoTrang.DataBindings.Clear();
            txt_TenDauSach.DataBindings.Clear();
            txt_TenKeSach.DataBindings.Clear();
           

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {

            txt_MaCuonSach.DataBindings.Add("Text", _dt, ".MACUONSACH");
            cbo_MaDauSach.DataBindings.Add("EditValue", _dt, ".MADAUSACH");
            txt_TenDauSach.DataBindings.Add("Text", _dt, ".TENDAUSACH");
            txt_NoiDungTomTat.DataBindings.Add("Text", _dt, ".NOIDUNG_TT");
            txt_NgonNgu.DataBindings.Add("Text", _dt, ".NGONNGU");
            txt_NamXuatBan.DataBindings.Add("Text", _dt, ".NAMXUATBAN");
            txt_LanXuatBan.DataBindings.Add("Text", _dt, ".LANXUATBAN");
            txt_KichThuoc.DataBindings.Add("Text", _dt, ".KICHTHUOC");
            txt_SoTrang.DataBindings.Add("Text", _dt, ".SOTRANG");
            cbo_MaKeSach.DataBindings.Add("EditValue", _dt, ".MAKESACH");
            txt_TenKeSach.DataBindings.Add("Text", _dt, "TENKESACH");
          
            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            //txtMaHV.Properties.ReadOnly = isReadOnly;
            txt_MaCuonSach.Properties.ReadOnly = isReadOnly;
            cbo_MaDauSach.Properties.ReadOnly = isReadOnly;
            cbo_MaKeSach.Properties.ReadOnly = isReadOnly;
           
            gridControl_CuonSach.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            ChangeStatus(false);
            ReadOnlyControl(false);
            ResetText();
            btnGoSearch.Visible = true;

            gridControl_CuonSach.Enabled = true;

            base.PerformSearch();
        }

        /// <summary>
        /// Execute Search data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnGoSearch_Click(object sender, EventArgs e)
        {

            var o = new DAL.Entities.CuonSach()
            {
                MACUONSACH = Convert.ToString(txt_MaCuonSach.Text),
                MADAUSACH = Convert.ToString(cbo_MaDauSach.EditValue),
                MAKESACH = Convert.ToString(cbo_MaKeSach.EditValue)

            };

            gridControl_CuonSach.DataSource = _dal.Search(o);

        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            _dt = _dal.Select();

             //Đổ dữ liệu vào Cbo
            cbo_MaDauSach.Properties.DataSource = _dt;
            cbo_MaKeSach.Properties.DataSource = _dt;
            try
            {
                if (_dt != null)
                {
                    gridControl_CuonSach.DataSource = _dt;

                    gridView_CuonSach.OptionsBehavior.ReadOnly = true;
                    gridView_CuonSach.OptionsView.ColumnAutoWidth = true;
                }

                base.LoadData();
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("Lỗi! Không load được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }

        }
        #endregion



    }
}
